const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    defaultAddress:'',
    goodsId:'',
    goodsDetailsdata:'',
    defaultCoupondata:'',
    ispay:true,
    totalprice:0,
    packFee:''
  },
  onLoad: function (option) {

  },

  adress(){
    var id=this.data.defaultAddress.id || ''
    wx.navigateTo({
      url: '/pages/product/adress'
    })
  },

  onShow: function () {

  },

  //支付
  pay() {
    wx.redirectTo({
      url: "/pages/product/order"
    })
    return
    if(!this.data.defaultAddress.id){
      wx.showToast({
        title: "请添加收货地址",
        icon: 'none',
        duration: 1000
      });
      return
    }
    Controller.order({goodsId:this.data.goodsId,addressId:(this.data.defaultAddress.id || ''),couponId:(this.data.defaultCoupondata.id || '')}).then(res=>{
      if(res.data.baseData){
        this.createWxPay(res.data.baseData)
      }
    })
  },
  createWxPay(orderNumber) {
    var that=this;
    let openId=app.globalData.openId
    Controller.createWxPay({ orderNumber: orderNumber, orderType: 2}).then(res => {
      let data=res.data.baseData;
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res){
          setTimeout(() => {
            wx.redirectTo({
              url: "/pages/product/order?ordernum="+orderNumber
            })
          },1000)
          wx.showToast({
            title: "支付成功",
            icon: 'none',
            duration: 1000
          });
        },
        fail(res){
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          },1000)
          wx.showToast({
            title: "支付失败",
            icon: 'none',
            duration: 1000
          });
        }
      })
    }).catch(res=>{
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      },1000)
      wx.showToast({
        title: "支付失败",
        icon: 'none',
        duration: 1000
      });
    })
  },

})